#!/bin/bash

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== LikeAdmin Python 开发环境启动脚本 ===${NC}"

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo -e "${RED}错误: Docker 未安装${NC}"
    exit 1
fi

# 检查Docker Compose是否安装
if ! command -v docker-compose &> /dev/null; then
    echo -e "${RED}错误: Docker Compose 未安装${NC}"
    exit 1
fi

# 提示配置Docker镜像源
echo -e "${YELLOW}提示: 为加快下载速度，建议配置Docker镜像源${NC}"
echo -e "${BLUE}配置文件位置:${NC}"
echo -e "  Linux: /etc/docker/daemon.json"
echo -e "  Windows: %programdata%\\docker\\config\\daemon.json"
echo -e "  macOS: ~/.docker/daemon.json"
echo -e "${BLUE}参考配置:${NC}"
echo -e "  cp daemon.json /etc/docker/daemon.json"
echo -e "  systemctl restart docker"
echo ""

# 创建必要的目录
echo -e "${YELLOW}创建必要的目录...${NC}"
mkdir -p data/mysql5.7.29/lib
mkdir -p data/redis
mkdir -p data/uploads
mkdir -p config/mysql

# 复制MySQL配置
if [ ! -f "config/mysql/mysqld.cnf" ]; then
    echo -e "${YELLOW}复制MySQL配置文件...${NC}"
    cp config/mysql/mysqld.cnf config/mysql/ 2>/dev/null || echo "MySQL配置文件不存在，将使用默认配置"
fi

# 停止现有服务
echo -e "${YELLOW}停止现有服务...${NC}"
docker-compose down

# 构建镜像
echo -e "${YELLOW}构建镜像...${NC}"
docker-compose build --no-cache

# 启动服务
echo -e "${YELLOW}启动开发服务...${NC}"
docker-compose up -d

# 等待服务启动
echo -e "${YELLOW}等待服务启动...${NC}"
sleep 30

# 检查服务状态
echo -e "${YELLOW}检查服务状态...${NC}"
docker-compose ps

# 显示访问信息
echo -e "${GREEN}=== 开发环境启动完成 ===${NC}"
echo -e "${GREEN}管理后台: http://localhost${NC}"
echo -e "${GREEN}管理后台API: http://localhost/api/admin${NC}"
echo -e "${GREEN}前台API: http://localhost/api/front${NC}"
echo -e "${GREEN}MySQL: localhost:3306${NC}"
echo -e "${GREEN}Redis: localhost:6379${NC}"

echo -e "${YELLOW}=== 常用命令 ===${NC}"
echo -e "${BLUE}查看日志: docker-compose logs -f${NC}"
echo -e "${BLUE}停止服务: docker-compose down${NC}"
echo -e "${BLUE}重启服务: docker-compose restart${NC}"
echo -e "${BLUE}进入容器: docker-compose exec fastapi-admin bash${NC}" 